
import 'package:flutter_map_demo/t3_map_plugin/map/core/t3_bubble_options.dart';
import 'package:flutter_map_demo/t3_map_plugin/map/core/t3_image_option.dart';
import 'package:flutter_map_demo/t3_map_plugin/map/core/t3_text_option.dart';

import 't3_base_overlay.dart';
import 't3_location.dart';

///此枚举控制显示图层层级
enum MapAnZindex {
  ///途经点
  middlePointZIndex(-100),
  ///当前点位点
  userLocationZIndex(-50),
  ///附近车车辆(在推荐点下方)
  nearbyCarZIndex(-10),
  ///推荐上车点
  recommendZIndex(100),
  /// 接中起点
  pickStartZIndex(-1),
  /// 送乘终点
  sendEndZIndex(-2),
  /// 接力单终点
  relayEndZIndex(-5),
  ///行程中接单车辆
  trafficLightsZIndex(200),
  ///行程中接单车辆
  routeCarZIndex(210),
  /// 行程中起点
  routerStartZIndex(220),
  ///等待接单
  waitPointZIndex(10080),
  ///单企业用车等待接单
  enterpriseWaitZIndex(10090),
  ///专车可点击起点
  specialNavStartZIndex(10100),
  ///路线标签
  routeTagZIndex(10111);

  final int value;
  const MapAnZindex(this.value);
}

/// 对应原生地图
class T3Annotation extends T3BaseOverlay {
  T3AnnotationOptions options;

  T3Annotation(this.options);

  update(T3AnnotationOptions options) {
    this.options = options;
  }

  Map<String, dynamic> toJson() {
    var json = options.toJson();
    json["uid"] = id;
    return json;
  }
}

class T3AnnotationOptions {
  final T3LocationCoordinate2D coordinate;
  final int zIndex;
  final T3AnnotationStyle style;
  final Map<String, dynamic>? args;

  T3AnnotationOptions({
    required this.coordinate,
    required this.zIndex,
    required this.style,
    this.args
  });

  Map<String, dynamic> toJson() {
    Map<String, dynamic> json = {
      'longitude' : coordinate.longitude,
      'latitude' : coordinate.latitude,
      'zIndex': zIndex,
      'style': style.value
    };

    if (args != null) {
      json["args"] = args;
    }
    return json;
  }

  factory T3AnnotationOptions.fromPin({
    required T3LocationCoordinate2D coordinate,
    required int zIndex,
    required T3PinAnnotationOptions pin}) {

      return T3AnnotationOptions(
        coordinate: coordinate,
        zIndex: zIndex,
        style: T3AnnotationStyle.pin,
          args: pin.toJson()
      );
  }

  factory T3AnnotationOptions.fromBubble({
    required T3LocationCoordinate2D coordinate,
    required int zIndex,
    required T3BubbleAnnotationOptions bubble
  }) {
    return T3AnnotationOptions(
        coordinate: coordinate,
        zIndex: zIndex,
        style: T3AnnotationStyle.bubble,
        args: bubble.toJson()
    );
  }

  factory T3AnnotationOptions.fromWait({
    required T3LocationCoordinate2D coordinate,
    required int zIndex,
    required T3WaitAnnotationOptions wait
  }) {
    return T3AnnotationOptions(
        coordinate: coordinate,
        zIndex: zIndex,
        style: T3AnnotationStyle.wait,
        args: wait.toJson()
    );
  }
}

/// 地图上点的类型
/// 原生根据此类型配置默认参数和初始化方法
/// 理解为不同的类
enum T3AnnotationStyle {
  pin("pin"),
  bubble("bubble"),
  wait("wait");

  final String value;
  const T3AnnotationStyle(this.value);
}

/// 气泡配置
class T3BubbleAnnotationOptions {
  /// 气泡描述
  /// 有些情况下气泡本身还没有展示但是刷新后会展示
  final T3BubbleOptions? bubbleOptions;
  /// 气泡下方图片
  final T3ImageOption? imageOption;
  /// 气泡下方文案
  final T3TextOption? textOption;

  T3BubbleAnnotationOptions({
    required this.bubbleOptions,
    this.imageOption,
    this.textOption,
  });

  Map<String, dynamic> toJson() {
    Map<String, dynamic> json = {
      "image": imageOption?.toJson(),
      "bubble": bubbleOptions?.toJson(),
      "text": textOption?.toJson(),
    };
    return json;
  }

  /// 单行文本的气泡
  factory T3BubbleAnnotationOptions.singleText({
    required T3TextOption bubbleTextOption,
    T3ImageOption? imageOption,
    T3TextOption? addressTextOption,
    bool showArrow = false
  }) {
    var bubbleOptions = T3BubbleOptions(
        texts: {'content' : bubbleTextOption},
        bubbleType: T3BubbleType.singleText,
        showArrow: showArrow
    );
    return T3BubbleAnnotationOptions(
        bubbleOptions: bubbleOptions,
        imageOption: imageOption,
        textOption: addressTextOption
    );
  }

  /// 预估气泡
  factory T3BubbleAnnotationOptions.valuation({
    required T3TextOption addressTextOption,
    T3TextOption? timeTextOption,
    T3TextOption? timeTipsTextOption,
    T3TextOption? stopInfoTextOption,
    required T3ImageOption imageOption,
    bool showArrow = true
  }) {
    var bubbleTexts = {
      'content' : addressTextOption
    };
    if (timeTextOption != null) {
      bubbleTexts['time'] = timeTextOption;
    }
    if (timeTipsTextOption != null) {
      bubbleTexts['timeTips'] = timeTipsTextOption;
    }

    if (stopInfoTextOption != null) {
      bubbleTexts['stopInfo'] = stopInfoTextOption;
    }
    var bubbleOptions = T3BubbleOptions(
        texts: bubbleTexts,
        bubbleType: T3BubbleType.valuation,
        showArrow: showArrow
    );
    return T3BubbleAnnotationOptions(
        bubbleOptions: bubbleOptions,
        imageOption: imageOption
    );
  }
}

class T3PinAnnotationOptions {
  final T3ImageOption imageOption;
  final T3TextOption? textOption;

  T3PinAnnotationOptions({required this.imageOption, this.textOption});

  ///起点大头针
  factory T3PinAnnotationOptions.start({String? addressText}) {

    final image = T3ImageOption(imageName: "map_icon_start_pin", width: 28, height: 28);
    final T3TextOption? text;
    if (addressText != null) {
      final style = T3TextStyle(color: "#1d2232", fontSize: 12, fontWeight: T3FontWeight.medium);
      text = T3TextOption(text: addressText, normalStyle: style);
    }
    else {
      text = null;
    }

    return T3PinAnnotationOptions(imageOption: image, textOption: text);
  }

  ///途经点大头针
  factory T3PinAnnotationOptions.via({String? addressText, String? iconName}) {
    String imageName = iconName ?? "map_icon_via_pin";
    final image = T3ImageOption(imageName: imageName, width: 28, height: 28);
    final T3TextOption? text;
    if (addressText != null) {
      final style = T3TextStyle(
          color: "#1d2232", fontSize: 12, fontWeight: T3FontWeight.medium);
      text = T3TextOption(text: addressText, normalStyle: style);
    } else {
      text = null;
    }

    return T3PinAnnotationOptions(imageOption: image, textOption: text);
  }

  ///终点大头针
  factory T3PinAnnotationOptions.end({String? addressText}) {
    final image = T3ImageOption(imageName: "map_icon_end_pin", width: 28, height: 28);
    final T3TextOption? text;
    if (addressText != null) {
      final style = T3TextStyle(color: "#1d2232", fontSize: 12, fontWeight: T3FontWeight.medium);
      text = T3TextOption(text: addressText, normalStyle: style);
    }
    else {
      text = null;
    }
    return T3PinAnnotationOptions(imageOption: image, textOption: text);
  }

  ///步行导航
  factory T3PinAnnotationOptions.way({String? addressText}) {
    final image = T3ImageOption(imageName: "icon_sharecar_off", width: 16, height: 16);
    return T3PinAnnotationOptions(imageOption: image, textOption: null);
  }

  Map<String, dynamic> toJson() {

    Map<String, dynamic> json = {
      "image": imageOption.toJson()
      };
    if (textOption != null) {
      json["text"] = textOption!.toJson();
    }
    return json;
  }
}

class T3WaitAnnotationOptions {
  /// 气泡
  final String? bubbleText;
  /// 气泡下方图片
  final String? image;
  /// 气泡下方文案
  final String? address;
  final double waterAnimationMultiple;
  final String? pulesColor;
  final String? limitStopTips;
  final String? guideMainUrl;
  final String? guidePageUrl;


  T3WaitAnnotationOptions({
    this.bubbleText,
    this.image,
    this.address,
    this.waterAnimationMultiple = 1.0,
    this.pulesColor,
    this.limitStopTips,
    this.guideMainUrl,
    this.guidePageUrl
  });

  Map<String, dynamic> toJson() {
    Map<String, dynamic> json = {
      "image": image,
      "bubbleText": bubbleText,
      "address": address,
      "waterAnimationMultiple": waterAnimationMultiple,
      "pulesColor": pulesColor,
      "limitStopTips": limitStopTips,
      "guideMainUrl": guideMainUrl,
      "guidePageUrl": guidePageUrl,
    };
    return json;
  }
}